package com.sl.water.entity;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@TableName("rehearsal_scheme") // 对应数据库表名
@Builder
public class RehearsalScheme extends Model<RehearsalScheme> {

    /**
     * 自增唯一标识
     */
    @TableId(type = IdType.AUTO)
    private Long id;
    /**
     * 预演类型(0人工预演，1自动预演，2典型场景,-1 快速预演)
     */
    @Schema(description = "预演类型:0人工预演，1自动预演，2典型场景,-1 快速预演")
    private Integer type;
    /**
     * 预报方案名称
     */
    @Schema(description = "方案名称")
    private String name;

    @Schema(description = "计算参数", example = "{\"inq\": [137, 137, 137, 137, 137, 137], \"time\": 6, \"rainfallProcess\": [63.2143, 28.7779, 0, 0, 0, 0]}")
    private String paramsJson;

    @Schema(description = "模拟时长")
    private Integer simulationDuration;

    @Schema(description = "不分洪模型参数")
    private String paramsBfh;

    @Schema(description = "分洪模型参数")
    private String paramsFh;

    @Schema(description = "分洪流量")
    private BigDecimal fhFlow;

    @Schema(description = "预演方案ID")
    private Long forecastId;

    /**
     * 不分洪状态，0为等待，1为计算中，2为计算完成，3为失败
     */
    @Schema(description = "不分洪状态")
    private Integer status1;
    /**
     * 分洪状态，0为等待，1为计算中，2为计算完成，3为失败
     */
    @Schema(description = "分洪状态")
    private Integer status2;
    /**
     * 异常信息
     */
    @Schema(description = "异常信息")
    private String error;

    @Schema(description = "闸门开启时间")
    private LocalDateTime zmkqsj;

    @Schema(description = "闸门开度")
    private Integer zmkd;

    /**
     * 创建时间
     */
    @Schema(description = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createdTime;
    /**
     * 最后更新时间
     */
    @Schema(description = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedTime;

    public Double _getRainFallTotal(){
        String params = null;
        if(StringUtils.isNotEmpty(this.getParamsBfh()))
            params = this.getParamsBfh();
        else
            params = this.getParamsFh();
        if(StringUtils.isNotEmpty(params)){
            JSONArray array=JSONUtil.parseArray(params);
            Double result = 0.0;
            for(int i=0;i<this.getSimulationDuration();i++){
                result = result + Double.valueOf(array.get(i).toString());
            }
            return result;
        }else
            return null;
    }

    public int _getZylxKind() {
        Double rainfall = this._getRainFallTotal();
        Double[]  kindArray= new Double[]{87.4546,99.0817,119.2776};
        if(rainfall>=kindArray[kindArray.length-1]){
            return kindArray.length-1;
        }
        for (int i = 0; i <kindArray.length-1; i++) {
            if(kindArray[i]>=rainfall){
                return i;
            }
        }
        return 0;
    }
}

